var util = require('../../utils/util.js');
var app = getApp();
var page = 1;
var hasMore = true;
var loadding = false;

var screnn_w = 320;

Page({
  data: {
    util: util,
    limit: 10,
    searchVal: "",
    huiyuan: 0,
    searchResult: [],
    content_item_w: 150,
    colorSet: ''
  },

  onLoad: function (data) {
    // uid = data.uid || "";
    page = 1;
    hasMore = true;
    loadding = false;

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        screnn_w = res.windowWidth;
      }
    })
    //获取颜色配置
    app.getColorSetData(function (color) {
      var col = color.goods_col ? parseInt(color.goods_col) : 3;
      col = col > 0 ? col : 3;
      that.setData({
        colorSet: color,
        // content_item_w: (screnn_w - (col + 1) * 5) / col
        content_item_w: screnn_w
      })
      that.resetNavColor();
    });

    var val = data.val;
    this.setData({
      huiyuan: data.huiyuan,
    })
    if (val) {
      this.setData({
        searchVal: val
      })
    }
    this.load();

    if (util.orgid == '17784') {
      this.loadAttributeGroups();
    }
  },

  loadMore: function () {
    var page;
    if (hasMore && !loadding) {
      page++;

      this.load();
    }
  },
  inputVal: function (e) {
    this.setData({
      searchVal: e.detail.value
    })
  },

  load: function () {
    loadding = true;
    var that = this;
    var source = this.data.source;
    var params = {
      publickey: util.publiccode,
      ac: "search",
      topictype: 4,
      keyword: that.data.searchVal,
      type: 1,
      page: page,
      num: that.data.limit
      // groupsel: 1
    };
    console.log("search请求参数：", params);
    util.api({
      data: params,
      success: function (responseData) {
        console.log(responseData);
        var resData = responseData.data;
        var newData;
        if (resData.msg) {
          that.setData({
            hasMore: false
          });
          if (page <= 1) {
            that.setData({
              searchResult: [],
            });
          }
          var tc = app.get_tc();
          wx.showToast({
            title: tc == 1 ? '沒有符合條件的數據' : '没有符合条件的数据',
          })
        } else {
          if (page <= 1) {
            that.setData({
              searchResult: resData,
            });
          } else {
            newData = that.data.searchResult.concat(resData);
            that.setData({
              searchResult: newData,
            });
          }
          loadding = false
        }
      }
    });
  },
  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },
  search: function () {
    page = 1;
    this.load();
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

  },
  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },
  // 读取主题
  loadAttributeGroups: function () {
    var that = this;
    var params = {
      publickey: util.publiccode,
      ac: "getAttributeGroups",
      orgid: util.orgid,
    }
    util.api({
      data: params,
      success: function (responseData) {
        var result = responseData.data;
        console.log("主题数据17784：", result);
        for (var i = 0; i < result.length; i++) {
          if (result[i].groupName == that.data.searchVal) {
            result[i].checked = true;
          } else {
            result[i].checked = false;
          }
        }
        that.setData({
          attrList: result
        });
      }
    });
  },
  //更多主题
  showMoreAttr: function () {
    this.setData({
      showMoreAttr: this.data.showMoreAttr == true ? false : true
    })
  },
  radioChange: function (e) {
    this.setData({
      searchVal: e.currentTarget.dataset.val
    })
    var result = this.data.attrList;
    for (var i = 0; i < result.length; i++) {
      if (result[i].groupName == e.currentTarget.dataset.val) {
        result[i].checked = true;
      } else {
        result[i].checked = false;
      }
    }
    this.setData({
      attrList: result
    })
    this.load();
  },
})